package org.nanhill.xmlro.gsp.model;

import java.util.Iterator;
import java.util.Map;

import org.nanhill.xmlro.Serializer;
import org.nanhill.xmlro.XmlroException;
import org.nanhill.xmlro.XmlroOutput;
import org.nanhill.xmlro.gsp.GspOutput;

/**
 * Serializing a java.util.Map.
 */
class GspMapSerializer implements Serializer {
	public void writeObject(Object obj, XmlroOutput out) throws XmlroException {
		GspOutput gout = (GspOutput) out;
		Map map = (Map) obj;
		gout.writeMapBegin();
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			Object objKey = iter.next();
			// Notice: gsp can't present object key, so use
			// object.toString() instead
			String key = objKey.toString();
			gout.writeMapMemberBegin(key);
			gout.writeObject(map.get(key));
			gout.writeMapMemberEnd(key);
		}
		gout.writeMapEnd();
	}
}
